package com.lzm.springcloud.Controller;

import com.lzm.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Description: TODO
 * @author: lzm
 * @date: 2021年04月22日 18:18
 */
@RestController
public class DeptConsumerController {
    //consumer shouldn't have service layer
    //RestFul

    @Autowired  //提供多种便捷访问远程http服务的方法  restful服务模板
    private RestTemplate restTemplate;
//    (url ,pojo/Map,request,responseTpe)
    private  static final  String Rest_URL_PREFIX="SPRINGCLOUD-PROVIDER-8001";
//  http://localhost:8001/dept/list
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id)
    {
        return restTemplate.getForObject(Rest_URL_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept)
    {
        return restTemplate.postForObject(Rest_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> list()
    {
        return restTemplate.getForObject(Rest_URL_PREFIX+"/dept/list",List.class);
    }
}
